#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

long double trait_values[3][7][15] = {
    { // class A
        {18, 17, 15, 16, 19, 15, 17, 18, 18, 14, 17, 15, 18, 16, 15}, // 1 
        {19, 16, 12, 11, 17, 18, 13, 11, 15, 16, 19, 17, 12, 13, 11}, // 2
        {22, 26, 38, 32, 24, 22, 34, 36, 26, 22, 30, 32, 38, 34, 24}, // 3
        {55, 56, 57, 57, 57, 59, 60, 60, 61, 62, 62, 63, 63, 64, 65}, // 4
        {35, 34, 30, 32, 37, 30, 33, 36, 35, 28, 33, 30, 35, 32, 29}, // 5
        {17, 18, 13, 19, 16, 12, 11, 17, 18, 13, 11, 15, 16, 19, 17}, // 6
        {11, 13, 19, 16, 12, 11, 17, 18, 13, 11, 15, 16, 19, 17, 12}  // 7
    },
    { // class B
        {27, 28, 25, 29, 24, 27, 22, 26, 27, 21, 25, 23, 26, 22, 24},
        {15, 16, 19, 17, 13, 19, 16, 12, 16, 16, 12, 11, 17, 18, 13},
        {26, 22, 30, 32, 39, 34, 26, 38, 32, 24, 22, 34, 36, 22, 28},
        {37, 38, 39, 39, 41, 41, 42, 42, 43, 44, 44, 45, 45, 46, 47},
        {53, 55, 50, 57, 47, 54, 43, 51, 55, 41, 49, 46, 52, 43, 47},
        {12, 19, 16, 12, 11, 17, 18, 13, 11, 15, 16, 19, 17, 12, 13},
        {13, 11, 15, 16, 19, 17, 13, 19, 16, 12, 11, 17, 18, 11, 14}
    },
    { // class C
        {30, 28, 32, 26, 30, 28, 31, 33, 25, 29, 27, 27, 26, 30, 28},
        {11, 15, 16, 19, 17, 12, 13, 11, 15, 16, 17, 17, 13, 19, 16},
        {34, 26, 38, 32, 24, 22, 34, 36, 22, 28, 26, 26, 38, 34, 24},
        {51, 52, 52, 53, 53, 54, 54, 54, 55, 56, 56, 57, 58, 58, 59},
        {60, 56, 64, 52, 59, 55, 61, 65, 50, 58, 63, 54, 51, 60, 56},
        {11, 15, 16, 19, 17, 13, 19, 16, 12, 11, 118, 18, 17, 18, 13},
        {17, 13, 19, 16, 12, 11, 17, 18, 11, 14, 13, 13, 19, 17, 12}
    }
};

int * get_correct_signs(long double trait_values[3][7][15]){
    int *max_signs = new int [2];
    long double max_elements[2];
    long double signs_info[3][7];
    long double average_sign[3][7];
    long double sqrt_average_sign[3][7];
    long double sum_info[7];

    // Получение среднего значения для всех признаков классов
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 7; j++){
            average_sign[i][j] = 0;
            for (int o = 0; o < 15; o++){
                average_sign[i][j] += trait_values[i][j][o];
            }
            average_sign[i][j] = average_sign[i][j] / 15;
        }
    }
    // Получение среднего квадратического значения для всех признаков классов
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 7; j++){
            sqrt_average_sign[i][j] = 0;
            for (int o = 0; o < 15; o++){
                sqrt_average_sign[i][j] += pow((trait_values[i][j][o] - average_sign[i][j]), 2);
            }
            sqrt_average_sign[i][j] = sqrt(sqrt_average_sign[i][j] / 15);
        }
    }

    // AB
    for (int j = 0; j < 7; j++){
        signs_info[0][j] = 0;
        signs_info[0][j] = (pow((average_sign[0][j] - average_sign[1][j]), 2)) / (pow(sqrt_average_sign[0][j], 2) + pow(sqrt_average_sign[1][j], 2));
    }

    // AC
    for (int j = 0; j < 7; j++){
        signs_info[1][j] = 0;
        signs_info[1][j] = (pow((average_sign[0][j] - average_sign[2][j]), 2)) / (pow(sqrt_average_sign[0][j], 2) + pow(sqrt_average_sign[2][j], 2));
    }

    // BC
    for (int i = 0; i < 7; i++){
        signs_info[2][i] = 0;
        signs_info[2][i] = (pow((average_sign[1][i] - average_sign[2][i]), 2)) / (pow(sqrt_average_sign[1][i], 2) + pow(sqrt_average_sign[2][i], 2));
    }

    for (int i = 0; i < 7; i++){
        sum_info[i] = 0;
    }

    for (int i = 0; i < 7; i++){
        sum_info[i] = signs_info[0][i] + signs_info[1][i] + signs_info[2][i];
    }

    // cout << "Информативность признаков\n\n";

    // for (int i = 0; i < 3; i++){
    //     for (int j = 0; j < 7; j++){
    //         cout << signs_info[i][j] << "\t\t";
    //     }
    //     cout << "\n";
    // }

    // cout << "\n";

    // for (int j = 0; j < 7; j++){
    //     cout << sum_info[j] << "\t\t";
    // }

    max_elements[0] = 0;
    max_elements[1] = 0;

    for (int i = 0; i < 7; i++){
        for (int j = 0; j < 7; j++){
            if (max_elements[0] < sum_info[j]){
                max_elements[0] = sum_info[j];
                max_signs[0] = j;
            }
        }
    }

    for (int i = 0; i < 7; i++){
        for (int j = 0; j < 7; j++){
            if (max_elements[1] < sum_info[j] && max_signs[0] != j){
                max_elements[1] = sum_info[j];
                max_signs[1] = j;
            }
        }
    }

    if (max_signs[0] > max_signs[1])
        swap(max_signs[0], max_signs[1]);

    return max_signs;
}

void defenition_class(int x, int y){
    vector <double> A;
    vector <double> B;
    vector <double> C;

    long double trait_matrix[45][3];

    long double centroids[3][2] = 
    {
        {
            {trait_values[0][x][0]},
            {trait_values[0][y][0]}
        },
        {
            {trait_values[1][x][0]},
            {trait_values[1][y][0]}
        },
        {
            {trait_values[2][x][0]},
            {trait_values[2][y][0]} 
        }
    };

    for (int j = 0; j < 45; j++){
        for (int i = 0; i < 3; i++){
            trait_matrix[j][i] = sqrt((pow(trait_values[i][x][j] - centroids[i][0], 2) + pow(trait_values[i][y][j] - centroids[i][1], 2)));
        }
    }


    cout << "\n\n";
    for (int i = 0; i < 45; i++){
        cout << "Obj " << i+1 << "\t\t\t\t";
        for (int j = 0; j < 3; j++){
            cout << "C " << j+1 << " = " << trait_matrix[i][j] << "\t\t\t\t";
        }
        cout << endl;
    }

}

int main(){
    int * signs;
    signs = get_correct_signs(trait_values);

    // cout << "\n\n" << "Выбранные признаки:\n";

    // for (int i = 0; i < 2; i++){
    //     cout << i+1 << ": " << signs[i]+1 << "\n";
    // }

    // cout << endl;

    defenition_class(signs[0], signs[1]);

    return 0;
}